package com.yiyun.blog.controller;

import com.yiyun.blog.common.aop.LogAnnotation;
import com.yiyun.blog.common.cache.Cache;
import com.yiyun.blog.service.ArticleService;
import com.yiyun.blog.vo.Result;
import com.yiyun.blog.vo.params.ArticleParam;
import com.yiyun.blog.vo.params.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Bao
 * @create 2022-01-01-18:56
 */
//json数据进行交互
@RestController
@RequestMapping("articles")
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    /*
    * 首页-文章列表
    *
    * */
    @PostMapping
    @LogAnnotation(module="文章",operater="获取文章列表")
//    @Cache(expire = 1 * 60 * 1000,name = "listArticle")
    public Result listArticle(@RequestBody PageParams pageParams){

        return articleService.listArticle(pageParams);
    }

    /**
     * 最热文章
     * */
    @PostMapping("hot")
//    @Cache(expire = 1 * 60 * 1000,name = "hot_article")
    public Result hotArticle(){

        int limit = 3;
        return articleService.hotArticle(limit);
    }

    /**
     * 最新文章
     * */
    @PostMapping("new")
    public Result newArticles(){

        int limit = 3;
        return articleService.newArticles(limit);
    }

    /**
     * 文章归档
     * */
    @PostMapping("listArchives")
    public Result listArchives(){

        return articleService.listArchives();
    }

    /**
     * 文章详情
     * */
    @PostMapping("view/{id}")
    //    @Cache(expire = 1 * 60 * 1000,name = "view_article")
    public Result findArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    /**
     * 发布文章
     * /articles/publish
     * */
    @PostMapping("publish")
    public Result publishArticle(@RequestBody ArticleParam articleParam){

        return articleService.publishArticle(articleParam);
    }

    /**
     * 修改文章
     * */
    @PostMapping("{id}")
    public Result updateArticleById(@PathVariable("id") Long articleId){
        return articleService.findArticleById(articleId);
    }

    // 搜索文章
    @PostMapping("search")
    public Result searchArticle(@RequestBody ArticleParam articleParam){

        return articleService.searchArticle(articleParam);
    }
}
